package com.dms.modules.user.controller;

import com.dms.common.api.ApiResponse;
import com.dms.modules.user.entity.Region;
import com.dms.modules.user.service.RegionService;
import com.dms.modules.user.vo.RegionTreeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user/region")
@Api(tags = "地区接口")
public class RegionController {

    @Autowired
    private RegionService regionService;

    @GetMapping("/provinces")
    @ApiOperation("获取省份列表")
    public ApiResponse<List<Region>> getProvinces() {
        return ApiResponse.success(regionService.getProvinces());
    }

    @GetMapping("/cities/{provinceCode}")
    @ApiOperation("获取城市列表")
    public ApiResponse<List<Region>> getCities(
            @ApiParam("省份编码") @PathVariable String provinceCode) {
        return ApiResponse.success(regionService.getCities(provinceCode));
    }

    @GetMapping("/districts/{cityCode}")
    @ApiOperation("获取区县列表")
    public ApiResponse<List<Region>> getDistricts(
            @ApiParam("城市编码") @PathVariable String cityCode) {
        return ApiResponse.success(regionService.getDistricts(cityCode));
    }

    @GetMapping("/{code}")
    @ApiOperation("根据编码获取区域信息")
    public ApiResponse<Region> getByCode(
            @ApiParam("区域编码") @PathVariable String code) {
        return ApiResponse.success(regionService.getByCode(code));
    }

    @GetMapping("/tree")
    @ApiOperation("获取完整的地区层级树")
    public ApiResponse<List<RegionTreeVO>> getRegionTree() {
        return ApiResponse.success(regionService.getRegionTree());
    }
}